package com.macro.mall.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.mapper.SmsPopupAdvertiseMapper;
import com.macro.mall.model.PmsProductExample;
import com.macro.mall.model.SmsPopupAdvertise;
import com.macro.mall.model.SmsPopupAdvertiseExample;
import com.macro.mall.service.PmsProductService;
import com.macro.mall.service.SmsPopupAdvertiseService;
import com.macro.mall.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 弹窗广告管理Service
 * Created by 高强 on 2020/8/28.
 */
@Service
public class SmsPopupAdvertiseServiceImpl implements SmsPopupAdvertiseService {

    @Autowired
    private SmsPopupAdvertiseMapper advertiseMapper;
    @Autowired
    private UmsAdminService adminService;
    @Autowired
    private PmsProductService productService;

    @Override
    public SmsPopupAdvertise creatOrUpdate(SmsPopupAdvertise advertise, String username) {
        //判断商品是否已审核
        boolean b = productService.whetherVerify(advertise.getProductId());
        if (!b) {
            return new SmsPopupAdvertise();
        }
        //根据用户名查询用户信息，得到userId，从redis中获取
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(username);
        //根据userId查询弹窗广告表中是否有对应弹窗广告，有则修改，无则新增
        SmsPopupAdvertiseExample advertiseExample = new SmsPopupAdvertiseExample();
        advertiseExample.createCriteria().andStoreIdEqualTo(admin.getStoreId());
        List<SmsPopupAdvertise> smsPopupAdvertiseList = advertiseMapper.selectByExample(advertiseExample);
        if (CollectionUtils.isEmpty(smsPopupAdvertiseList)) {
            // 当前门店没有对应弹窗广告
            advertise.setUserId(admin.getId());
            advertise.setStoreId(admin.getStoreId());
            advertise.setStatus(1);//状态设为开启
            //新增
            advertiseMapper.insert(advertise);
            return advertise;
        } else {
            // 当前门店有对应弹窗广告
            SmsPopupAdvertise bo = smsPopupAdvertiseList.get(0);
            bo.setProductId(advertise.getProductId());
            bo.setProductName(advertise.getProductName());
            bo.setProductPic(advertise.getProductPic());
            bo.setStatus(advertise.getStatus());
            //更改
            advertiseMapper.updateByPrimaryKey(bo);
            return bo;
        }
    }

    @Override
    public int updateStatus(Long id,Integer status) {
        SmsPopupAdvertise advertise = new SmsPopupAdvertise();
        advertise.setId(id);
        advertise.setStatus(status);
        return advertiseMapper.updateByPrimaryKey(advertise);
    }
    /**
     * 根据店铺获取弹窗广告信息
     */
    @Override
    public CommonResult get(String currentUsername) {
       UmsAdminDTO umsAdminDTO =  adminService.getAdminDTOByUserName(currentUsername);
        SmsPopupAdvertiseExample advertiseExample = new SmsPopupAdvertiseExample();
        List<SmsPopupAdvertise> smsPopupAdvertises = new ArrayList<>();
        if(umsAdminDTO.getStoreId() == null){
            return CommonResult.failed("当前用户暂无门店");
        }
        advertiseExample.createCriteria().andStoreIdEqualTo( umsAdminDTO.getStoreId());
        smsPopupAdvertises = advertiseMapper.selectByExample(advertiseExample);
        return CommonResult.success(smsPopupAdvertises);
    }
}
